package com.quanshu.exchange.ui.gui.qrcode;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.google.gson.Gson;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.qrcode.utils.QrUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jye on 2018/1/30.
 */

public class MyQrCodeActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.my_qr_image)
    ImageView myQrImage;
    @BindView(R.id.avatar)
    ImageView avatar;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.desc)
    TextView desc;

    private int type;
    private GroupEntity groupEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_qrcode);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        type = getIntent().getIntExtra("type", 0);
        groupEntity = (GroupEntity) getIntent().getSerializableExtra("GroupEntity");
    }

    private void initView() {

        // 用户
        if (type == 1) {

            title.setText("我的二维码");
            name.setText(AppUtils.getUserName(currentUser));

            GlideUtils.setRoundImageView(this, currentUser.getAvatar(), R.drawable.default_avatar, avatar);
            desc.setText("扫一扫，加我好友");

            ScanQrCode qr = new ScanQrCode();
            qr.setType(1);
            qr.setValue(currentUser.getMemberid());
            Gson gson = new Gson();

            Bitmap mBitmap = QrUtils.createImage(gson.toJson(qr), 1000, 1000, null);
            myQrImage.setImageBitmap(mBitmap);

        } else if (type == 2 && groupEntity != null) {

            title.setText("群二维码");
            name.setText(groupEntity.getGroup_name());

            GlideUtils.setRoundImageView(this, groupEntity.getGroup_photo(), R.drawable.default_avatar, avatar);
            desc.setText("扫一扫，加入群聊");

            ScanQrCode qr = new ScanQrCode();
            qr.setType(2);
            qr.setValue(groupEntity.getHx_groupid());
            Gson gson = new Gson();

            Bitmap mBitmap = QrUtils.createImage(gson.toJson(qr), 1000, 1000, null);
            myQrImage.setImageBitmap(mBitmap);
        }
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
    }

}
